package com.oss.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.oss.model.Project;
import com.oss.validation.ProjectValidator;

/**
 * @author xueqiang.mi
 * @date 2010-2-2
 */
@Controller
public class ProjectController extends BaseController {

	@RequestMapping(value = "/shareProject.do", method = RequestMethod.POST)
	public String addHandler(Project project, BindingResult result,
			SessionStatus status) {
		new ProjectValidator().validate(project, result);
		if (result.hasErrors()) {
			return "projectForm";
		} else {
			// set created author for project
			UserService userService = UserServiceFactory.getUserService();
			User user = userService.getCurrentUser();
			if (user != null) {
				project.setCreatedAuthor(user);
				// enables the projects added by google user
				project.setEnabled(true);
			} else {
				// disables the projects added by anonymous
			}
			prjService.add(project);

			status.setComplete();
			return "redirect:/tag.do?id=" + project.getTag();
		}
	}

	@RequestMapping(value = "/shareProject.do", method = RequestMethod.GET)
	public String addProject(Model model,
			@RequestParam(value = "tid", required = false) String tid) {
		Project prj = new Project();
		prj.setTag(tid);
		model.addAttribute(prj);

		return "projectForm";
	}

	@RequestMapping("/project.do")
	public String prj(
			Model model,
			@RequestParam(value = "id") String id,
			@RequestParam(value = "pageIndex", required = false) Long pageIndex,
			@RequestParam(value = "pageSize", required = false) Long pageSize) {
		if (pageIndex == null || pageSize == null) {
			return "redirect:/project.do?id=" + id + "&pageIndex=1&pageSize="
					+ PRJ_MEDIUM_SIZE;
		}
		pageIndex = pageIndex > 0 ? pageIndex : 1;
		pageSize = pageSize > 50 ? 50 : (pageSize > 0 ? pageSize
				: BaseController.PRJ_MEDIUM_SIZE);
		model.addAttribute("prj", prjService.get(id));
		model.addAttribute("links", lnkService.getLnksByPrj("user", id,
				pageIndex, pageSize));
		return "project";
	}

	@RequestMapping("/projects.do")
	public String prjs(
			Model model,
			@RequestParam(value = "pageIndex", required = false) Long pageIndex,
			@RequestParam(value = "pageSize", required = false) Long pageSize) {
		if (pageIndex == null || pageSize == null) {
			return "redirect:/projects.do?pageIndex=1&pageSize="
					+ PRJ_MEDIUM_SIZE;
		}
		pageIndex = pageIndex > 0 ? pageIndex : 1;
		pageSize = pageSize > 50 ? 50 : (pageSize > 0 ? pageSize
				: BaseController.PRJ_MEDIUM_SIZE);
		model.addAttribute("prjs", prjService
				.getLatestPrjs(pageIndex, pageSize));
		return "projects";
	}

}
